Skip to content

Fix AttributeError when accessing Response.text with Payload body#12239

Open
wavebyrd wants to merge 3 commits intoaio-libs:masterfrom
wavebyrd:fix-response-text-attributeerror-2928
Open

Fix AttributeError when accessing Response.text with Payload body#12239
wavebyrd wants to merge 3 commits intoaio-libs:masterfrom
wavebyrd:fix-response-text-attributeerror-2928

Conversation

@wavebyrd
Copy link
Contributor

Summary

When Response.body is set to a value that gets converted to a Payload without a decode method (e.g., AsyncIterablePayload, BodyPartReader), accessing Response.text would raise an unhelpful AttributeError:

>>> from aiohttp.web_response import Response
>>> r = Response()
>>> r.body = some_async_iterator
>>> r.text
AttributeError: 'AsyncIterablePayload' object has no attribute 'decode'

Changes

  • Modified Response.text property to check for bytes/bytearray first
  • For Payload objects, checks if decode method exists before calling it
  • Raises a clear TypeError with guidance when the body type doesn't support text access

Test Plan

  • Existing tests in test_payload_body_get_text already cover this case
  • The test expects TypeError for payloads like async_iter() and BodyPartReader

Fixes #2928

When Response.body is set to a value that gets converted to a Payload
without a decode method (e.g., AsyncIterablePayload, BodyPartReader),
accessing Response.text would raise an AttributeError.

Now properly checks for the decode method and raises a clear TypeError
with guidance on using the text setter for string content.

Fixes aio-libs#2928

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 14, 2026
@codecov
Copy link

codecov bot commented Mar 14, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.10%. Comparing base (d9f9207) to head (ebf7b86).
⚠️ Report is 6 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
aiohttp/web_response.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12239      +/-   ##
==========================================
- Coverage   99.11%   99.10%   -0.01%     
==========================================
  Files         130      130              
  Lines       45373    45407      +34     
  Branches     2397     2400       +3     
==========================================
+ Hits        44970    45002      +32     
- Misses        271      274       +3     
+ Partials      132      131       -1     
Flag Coverage Δ
CI-GHA 98.96% <75.00%> (-0.01%) ⬇️
OS-Linux 98.70% <75.00%> (-0.02%) ⬇️
OS-Windows 96.97% <75.00%> (-0.01%) ⬇️
OS-macOS 97.85% <75.00%> (-0.02%) ⬇️
Py-3.10.11 97.40% <75.00%> (-0.01%) ⬇️
Py-3.10.19 ?
Py-3.10.20 97.89% <75.00%> (-0.01%) ⬇️
Py-3.11.14 ?
Py-3.11.15 98.09% <75.00%> (-0.01%) ⬇️
Py-3.11.9 97.61% <75.00%> (-0.01%) ⬇️
Py-3.12.10 97.70% <75.00%> (-0.01%) ⬇️
Py-3.12.12 ?
Py-3.12.13 98.17% <75.00%> (-0.01%) ⬇️
Py-3.13.12 98.43% <75.00%> (-0.01%) ⬇️
Py-3.14.3 98.48% <75.00%> (-0.01%) ⬇️
Py-3.14.3t 97.49% <75.00%> (-0.01%) ⬇️
Py-pypy3.11.13-7.3.20 97.52% <75.00%> (-0.02%) ⬇️
VM-macos 97.85% <75.00%> (-0.02%) ⬇️
VM-ubuntu 98.70% <75.00%> (-0.02%) ⬇️
VM-windows 96.97% <75.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 14, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing wavebyrd:fix-response-text-attributeerror-2928 (ebf7b86) with master (a618a87)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (6f059db) during the generation of this report, so a618a87 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@wavebyrd
Copy link
Contributor Author

Ping - this fixes AttributeError when Response.text is accessed with Payload body. Let me know if any changes needed!

@Dreamsorcerer
Copy link
Member

Ping - this fixes AttributeError when Response.text is accessed with Payload body. Let me know if any changes needed!

Do you not see the comment above?

Address review feedback: use try/except to catch AttributeError
instead of isinstance/hasattr checks, which is more Pythonic and
has no performance overhead when successful.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@wavebyrd
Copy link
Contributor Author

Thanks for the review feedback! I've updated to use the try/except pattern - much cleaner. The isinstance check for bytes/bytearray was redundant since they have decode() anyway.

@Dreamsorcerer Dreamsorcerer added backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot labels Mar 17, 2026
@Dreamsorcerer
Copy link
Member

Can we get a test to cover the new code?

themavik

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing Response.text can fail with an AttributeError

3 participants